﻿#region "Copyright (C) Lenny Granovsky. 2011-2013"
//    This library provides extended cache capabilities to the ASP.NET applications.
//
//                Copyright (C) Lenny Granovsky. 2011-2013. 
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//    This program comes with ABSOLUTELY NO WARRANTY.
//    This is free software, and you are welcome to redistribute it
//    under certain conditions;
#endregion

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading;

namespace FlexiCache
{
    /// <summary>
    /// Class provides part of HttpSession class functionality to  store objects as session 
    /// variable in either current HttpContext, if available, or, alternatively, in Thread memory data slot. 
    /// This allows to use HttpSession object collection storage from within windows applications, 
    /// including NUnit tests.
    /// This class is not intended to be used directly. The RuntimeContext should be used instead.
    /// </summary>
    [Serializable()]
    public sealed class RuntimeContextSessionStorage
    {
        private const string SessionContextStorageMemorySlotName = "RuntimeContextSessionStorage";

        private static SessionObjectCollection ThreadSessionData
        {
            get
            {
                LocalDataStoreSlot slot = Thread.GetNamedDataSlot(SessionContextStorageMemorySlotName);
                object tmp = Thread.GetData(slot);
                if (tmp != null)
                    return (SessionObjectCollection)tmp;
                Thread.SetData(slot, new SessionObjectCollection());
                tmp = Thread.GetData(slot);
                if (tmp != null)
                    return (SessionObjectCollection)tmp;
                throw new Exception("Unable to initialize SessionObjectCollection for session data in thread memory data slot.");
            }
        }

        public RuntimeContextSessionStorage()
        {
        }

        public void Add(string name, object value)
        {
            if (System.Web.HttpContext.Current != null)
                System.Web.HttpContext.Current.Session.Add(name, value);
            else
                ThreadSessionData.Add(name, value);
        }

        public void Clear()
        {
            if (System.Web.HttpContext.Current != null)
                System.Web.HttpContext.Current.Session.Clear();
            else
                ThreadSessionData.Clear();
        }

        public IEnumerator GetEnumerator()
        {
            if (System.Web.HttpContext.Current != null)
                return System.Web.HttpContext.Current.Session.GetEnumerator();
            else
                return ThreadSessionData.GetEnumerator();
        }

        public void Remove(string name)
        {
            if (System.Web.HttpContext.Current != null)
                System.Web.HttpContext.Current.Session.Remove(name);
            else
                ThreadSessionData.Remove(name);
        }

        public void RemoveAll()
        {
            this.Clear();
        }

        public void RemoveAt(int index)
        {
            if (System.Web.HttpContext.Current != null)
                System.Web.HttpContext.Current.Session.RemoveAt(index);
            else
                ThreadSessionData.RemoveAt(index);
        }

        public int Count
        {
            get
            {
                if (System.Web.HttpContext.Current != null)
                    return System.Web.HttpContext.Current.Session.Count;
                else
                    return ThreadSessionData.Count;
            }
        }

        public object this[int index]
        {
            get
            {
                if (System.Web.HttpContext.Current != null)
                    return System.Web.HttpContext.Current.Session[index];
                else
                    return ThreadSessionData[index];
            }
            set
            {
                if (System.Web.HttpContext.Current != null)
                    System.Web.HttpContext.Current.Session[index] = value;
                else
                    ThreadSessionData[index] = value;
            }
        }

        public object this[string name]
        {
            get
            {
                if (System.Web.HttpContext.Current != null)
                    return System.Web.HttpContext.Current.Session[name];
                else
                    return ThreadSessionData[name];
            }
            set
            {
                if (System.Web.HttpContext.Current != null)
                    System.Web.HttpContext.Current.Session[name] = value;
                else
                    ThreadSessionData[name] = value;
            }
        }

        public NameObjectCollectionBase.KeysCollection Keys
        {
            get
            {
                if (System.Web.HttpContext.Current != null)
                    return System.Web.HttpContext.Current.Session.Keys;
                else
                    return ThreadSessionData.Keys;
            }
        }

        public bool Contains(string name)
        {
            if (System.Web.HttpContext.Current != null)
            {
                IEnumerator en = System.Web.HttpContext.Current.Session.Keys.GetEnumerator();
                while (en.MoveNext())
                {
                    if (en.Current.ToString() == name)
                        return true;
                }
                return false;
            }
            else
                return ThreadSessionData.Contains(name);
        }

        public bool ContainsAndNotNull(string name)
        {
            if (!this.Contains(name))
                return false;
            return (this[name] != null);
        }
    }
}
